<?php

/* ========================================================================= */
/* This file is part of the HypnoPHP Framework Library                       */
/* Copyright (c) 2009-2010, Radu Buzea                                       */
/* ========================================================================= */

/**
* @author Radu Buzea
* @copyright Copyright (c) 2009-2010, Radu Buzea (www.hypnosis.ro)
* @license GNU Lesser General Public License (www.gnu.org/copyleft/lesser.html)
* @package HypnoPHP
* @category Core
* @version 1.1.2
*/

defined('HYPNOPHP_APPLICATION') or
die("Cannot use <i>" . basename(__FILE__) . "</i> outside HypnoPHP Application");

# ###########################################################################

/**
* Convert any kind value type to boolean value
*
* @param mixed $param
* @return bool
*/
function boolval($param)
{
	if (is_bool($param)) { return $param; }
	if (is_numeric($param)) { return ($param > 0); }
	if (is_string($param)) {
		$strchk = array('true' => 1, 'on' => 2, 'ok' => 3, 'yes' => 4);
		return isset($strchk[trim(strtolower($param))]);
	}
	return empty($param);
}

# ###########################################################################

/**
* Output data inside XMP HTML tags or in a specific format
*
* @param mixed $param
* @param string $format_output The optional output format
* @return void
*/
function out($param = null, $format_output = '')
{
	if (is_null($param)) { $param = 'NULL'; }
	else if (is_bool($param)) {
		if ($param) { $param = 'TRUE'; }
		else { $param = 'FALSE'; }
	}
	$param = print_r($param, true);
	if ($format_output != '') {
		printf($format_output, $param);
	} else {
		echo "<xmp>{$param}</xmp>" . PHP_EOL;
	}
}

# ###########################################################################

/**
* Returns data inside XMP HTML tags or in a specific format
*
* @param mixed $param
* @param string $format_output The optional output format
* @return string
*/
function pout($param = null, $format_output = '')
{
	if (is_null($param)) { $param = 'NULL'; }
	else if (is_bool($param)) {
		if ($param) { $param = 'TRUE'; }
		else { $param = 'FALSE'; }
	}
	$param = print_r($param, true);
	if ($format_output != '') {
		return sprintf($format_output, $param);
	} else {
		return "<xmp>{$param}</xmp>" . PHP_EOL;
	}
}

# ###########################################################################

/**
* Generate a PHP script for an array
*
* @param array $data
* @param string $result Append the generated PHP code to the result
* @param integer $indent_inc Indent the PHP code
* @return string
*/
function array2php(array $data = null, $result = '', $indent_inc = 1)
{
	if (!is_array($data) || count($data) == 0) { return $result; }
	if ($result == '') { $result = "array(" . PHP_EOL; }
	$indent = "";
	while (strlen($indent) < $indent_inc) { $indent .= "\t"; }
	foreach ($data as $key => $value) {
		$key = addslashes(stripslashes($key));
		$result .= $indent . '"' . $key . '" => ';
		$type = gettype($value);
		switch ($type) {
			case 'integer':
			case 'double':
				$result .= $value . "," . PHP_EOL;
				break;
			case 'boolean':
				$result .= ($value ? "true" : "false") . "," . PHP_EOL;
				break;
			case 'string':
				$value = addslashes(stripslashes($value));
				$result .= '"' . $value . '",' . PHP_EOL;
				break;
			case 'array':
				$result .= "array(" . PHP_EOL;
				$result = array2php($value, $result, $indent_inc + 1) . "," . PHP_EOL;
				break;
			case 'object':
				$result .= '"' . addslashes(serialize($value)) . '",' . PHP_EOL;
				break;
			default:
				$result .= "null," . PHP_EOL;
				break;
		}
	}
	if ($indent_inc > 1) {
		for ($i = 0; $i < $indent_inc - 1; $i++) { $result .= "\t"; }
	}
	return $result . ")";
}

# ###########################################################################

/**
* Generate a PHP script for an array and save the code in a file
*
* @param array $data
* @param string $var_name The variable name that will be associated to the array
* @param string $file_name The destination file name
* @param string $append_data The data that has to be appended to the code
* @return bool
*/
function array2php_file(array $data = null, $var_name = '', $file_name = '', $append_data = '')
{
	$var_name = trim($var_name);
	$file_name = trim($file_name);
	if ($var_name == '' || $file_name == '') { return false; }
	$content = '<' . '?php' . PHP_EOL .
		'global $' . $var_name . ';' . PHP_EOL .
		'$' . $var_name . ' = ' . array2php($data) . ';' . PHP_EOL;
	if (trim($append_data) != '') { $content .= $append_data . PHP_EOL; }
	return HFileSys::writeFile($content, $file_name);
}

# ###########################################################################
